//
//  ViewController.swift
//  C015_test_CALayer
//
//  Created by yao_yu on 15/6/19.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var testView: UIView!
    let testLayer = CALayer()
    let testWidth:CGFloat = 100

    override func viewDidLoad() {
        super.viewDidLoad()
        
        testView = UIView(frame: CGRectZero)
        testView.backgroundColor = UIColor.blueColor()
        view.addSubview(testView)
        drawMyLayer()
    }
    
    override func viewDidLayoutSubviews() {
        let edgeSize:CGFloat = 50
        testView.frame = CGRectMake(edgeSize, edgeSize, view.frame.width - edgeSize * 2, 200)
        testLayer.position = CGPointMake(CGRectGetMidX(testView.bounds), CGRectGetMidY(testView.bounds) )
    }
    
    func drawMyLayer() {
        
        let layer = testLayer
        layer.backgroundColor = UIColor(red: 0, green: 144/255, blue: 1.0, alpha: 1.0).CGColor
        layer.position = CGPointMake(CGRectGetMidX(testView.bounds), CGRectGetMidY(testView.bounds) )
        layer.bounds = CGRectMake(0, 0, testWidth, testWidth)
        layer.cornerRadius = testWidth / 2
        layer.shadowColor = UIColor.grayColor().CGColor
        layer.shadowOffset = CGSizeMake(2, 2)
        layer.shadowOpacity = 0.9
        
        testView.layer.addSublayer(layer)

    }
    
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        var width = testLayer.bounds.size.width
        if width == testWidth {
            width = testWidth * 4
        } else {
            width = testWidth
        }
        testLayer.bounds = CGRectMake(0, 0, width, width)
        testLayer.cornerRadius = width / 2
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

